package com.google.code.drift.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.alibaba.fastjson.JSONObject;
import com.google.code.drift.R;
import com.google.code.drift.api.AppSkyApp;
import com.google.code.drift.utils.ImageUtils;
import com.google.code.drift.utils.JsonFactory;
import com.google.code.drift.utils.CommonUtils;
import com.google.code.drift.utils.PreferencesProxy;
import com.google.code.drift.utils.PreferencesProxy.PrefKey;

public class EditUserInfoActivity extends Activity implements OnClickListener {

	AppSkyApp app;

	ImageView avatarImage;
	EditText nickEdit;
	RadioGroup sexChooserGroup;
	Button positiveButton;
	Button negativeButton;

	/**
	 * 性别值和其所在RadioButton的id的对应数组
	 */
	int[] sexToIdArray = new int[3];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_user_info_layout);
		setTitle("编辑信息");

		app = (AppSkyApp) getApplication();

		initView();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.edit_button_ok:
			verifyAndUpdate();
			finish();
			break;
		case R.id.edit_button_cancel:
			finish();
			break;
		default:
			break;
		}

	}

	private void initView() {

		avatarImage = (ImageView) findViewById(R.id.edit_avatar);
		nickEdit = (EditText) findViewById(R.id.edit_input_nick);
		sexChooserGroup = (RadioGroup) findViewById(R.id.edit_sex_chooser);
		positiveButton = (Button) findViewById(R.id.edit_button_ok);
		negativeButton = (Button) findViewById(R.id.edit_button_cancel);

		initSexToId();

		setupView();
	}

	private void initSexToId() {
		sexToIdArray[0] = R.id.sex_chooser_female;
		sexToIdArray[1] = R.id.sex_chooser_male;
		sexToIdArray[2] = R.id.sex_chooser_secret;
	}

	private void setupView() {
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.a_avatar);
		bitmap = ImageUtils.getRoundedCornerBitmap(bitmap);
		avatarImage.setImageBitmap(bitmap);

		nickEdit.setHint(app.getMyInfo().getNick());

		sexChooserGroup.check(sexToIdArray[app.getMyInfo().getSex()]);

		positiveButton.setOnClickListener(this);
		negativeButton.setOnClickListener(this);
	}

	private void verifyAndUpdate() {
		boolean isSexChanged = false;
		boolean isNickChanged = false;
		int id = sexChooserGroup.getCheckedRadioButtonId();
		int newSex = getIndexFromId(id);
		int oldSex = app.getMyInfo().getSex();
		if (oldSex != newSex) {
			app.getMyInfo().setSex(newSex);
			PreferencesProxy.putInt(PrefKey.MY_SEX, newSex);
			isSexChanged = true;
		}
		String oldNick = app.getMyInfo().getNick();
		String nick = nickEdit.getText().toString().trim();
		if (CommonUtils.isNotNullString(nick) && !oldNick.equals(nick)) {
			app.getMyInfo().setNick(nick);
			PreferencesProxy.putString(PrefKey.MY_NICK, nick);
			isNickChanged = true;
		}
		if (isSexChanged || isNickChanged) {
			JsonFactory jsonFactory = JsonFactory.getFactory();
			JSONObject json = jsonFactory.makeUpdateInfoJson(app.getMyInfo()
					.getUserId(), app.getMyInfo().getNick(), app.getMyInfo()
					.getSex());
			CommonUtils.sendDataToServer(EditUserInfoActivity.this,
					json.toJSONString());
		}
	}

	private int getIndexFromId(int id) {
		int length = sexToIdArray.length;
		int index = 0;
		for (; index < length; index++) {
			if (sexToIdArray[index] == id) {
				break;
			}
		}
		return index;
	}

}
